import ExcelJS from 'exceljs'

export async function parseExcel(filename: string) {
  const workbook = new ExcelJS.Workbook() // 创建Excel工作簿
  await workbook.xlsx.readFile(filename) // 读取Excel文件
  const worksheet = workbook.getWorksheet(1); // 获取第一个工作表
  const exams = []
  worksheet.eachRow((row, rowNumber) => {
    if (rowNumber <= 2) return
    if (rowNumber === 3) {
      exams.push({
        questionIndex: rowNumber - 3,
        questionType: row.values[1],
        questionTitle: row.values[2],
        questionOptions: row.values[3],
        questionCorrectAnswer: row.values[4],
        questionScore: row.values[5],
        questionAnalysis: row.values[6],
      })
    } else {
      console.log(`Row ${rowNumber}:`, row.values); // 打印每一行的数据
      exams.push({
        questionIndex: rowNumber - 3,
        questionType: row.values[1],
        questionTitle: row.values[2],
        questionOptions: (row.values[3] as string)?.split('\n') || [],
        questionCorrectAnswer: row.values[4],
        questionScore: row.values[5],
        questionAnalysis: row.values[6],
      })
    }
  })

  return exams
}